package com.eastfair.pay.controller;

import com.eastfair.pay.dto.ChinaumsLogDTO;
import com.eastfair.pay.entity.ChinaumsLog;
import com.eastfair.pay.dto.ChinaumsLogSaveDTO;
import com.eastfair.pay.dto.ChinaumsLogUpdateDTO;
import com.eastfair.pay.dto.ChinaumsLogPageQuery;
import com.eastfair.pay.enumeration.TransactionOrderOrderTypeEnum;
import com.eastfair.pay.service.ChinaumsLogService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.pay.vo.ChinaumsLogVO;
import com.eastfair.pay.vo.CollectRecordVO;
import com.eastfair.venuebooking.vo.ExhibitionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 调用银联日志
 * </p>
 *
 * @author linan
 * @date 2023-05-05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/chinaumsLog")
@Api(value = "ChinaumsLog", tags = "调用银联日志")
@PreAuth(replace = "pay:chinaumsLog:")
public class ChinaumsLogController extends SuperController<ChinaumsLogService, Long, ChinaumsLog, ChinaumsLogPageQuery, ChinaumsLogSaveDTO, ChinaumsLogUpdateDTO> {

    @Value("${com.eastfair.projectId}")
    private Long projectId;
    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ChinaumsLog> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ChinaumsLog> chinaumsLogList = list.stream().map((map) -> {
            ChinaumsLog chinaumsLog = ChinaumsLog.builder().build();
            //TODO 请在这里完成转换
            return chinaumsLog;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(chinaumsLogList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ChinaumsLogPageQuery> params) {
        ChinaumsLogPageQuery pageQuery = params.getModel();

    }

    /**
     * 公众号发起支付，获取银联下单url
     * @param chinaumsLog ChinaumsLogDTO
     * @return R
     */
    @ApiOperation(value = "公众号发起支付，获取银联下单url", notes = "公众号发起支付，获取银联下单url")
    @PostMapping("/initiationOfPayments")
    public R<ChinaumsLogVO> initiationOfPayments(@RequestBody ChinaumsLogDTO chinaumsLog) {
        log.info("initiationOfPayments - 公众号发起支付，获取银联下单url chinaumsLog：{}",chinaumsLog);
        try{
            return R.success(baseService.initiationOfPayments(chinaumsLog));
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * C扫B发起支付，获取二维码URL
     * @param chinaumsLog ChinaumsLogDTO
     * @return R
     */
    @ApiOperation(value = "C扫B发起支付，获取二维码URL", notes = "C扫B发起支付，获取二维码URL")
    @PostMapping("/getPaymentQrCode")
    public R<ChinaumsLogVO> getPaymentQrCode(@RequestBody ChinaumsLogDTO chinaumsLog) {
        log.info("getQrcode - C扫B发起支付，获取二维码URL chinaumsLog：{}",chinaumsLog);
        try {
            return R.success(baseService.getPaymentQrCode(chinaumsLog));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 小程序支付下单
     * @param chinaumsLog ChinaumsLogDTO
     * @return R
     */
    @ApiOperation(value = "小程序支付下单", notes = "小程序支付下单")
    @PostMapping("/getMiniPayRequest")
    public R<ChinaumsLogVO> getMiniPayRequest(@RequestBody @Validated(ChinaumsLogDTO.MiniPay.class) ChinaumsLogDTO chinaumsLog) {
        log.info("getMiniPayRequest - 小程序支付下单 chinaumsLog：{}",chinaumsLog);
        try {
            return R.success(baseService.getMiniPayRequest(chinaumsLog));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 网关支付下单
     * @param chinaumsLog ChinaumsLogDTO
     * @return R
     */
    @ApiOperation(value = "网关支付下单", notes = "网关支付下单")
    @PostMapping("/getUpgPayRequest")
    public R<ChinaumsLogVO> getUpgPayRequest(@RequestBody ChinaumsLogDTO chinaumsLog) {
        log.info("getUpgPayRequest - 网关支付下单 chinaumsLog：{}",chinaumsLog);
        try {
            return R.success(baseService.getUpgPayRequest(chinaumsLog));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询支付状态
     * @param chinaumsLog ChinaumsLogDTO
     * @return R
     */
    @ApiOperation(value = "查询支付状态", notes = "查询支付状态")
    @PostMapping("/queryPayStatus")
    public R<ChinaumsLogVO> queryPayStatus(@RequestBody ChinaumsLogDTO chinaumsLog) {
        log.info("queryPayStatus - 查询支付状态 chinaumsLog：{}",chinaumsLog);
        return R.success(baseService.queryPayStatus(chinaumsLog));
    }

    /**
     * 退款
     * @param chinaumsLog ChinaumsLogDTO
     * @return R
     */
    @ApiOperation(value = "退款", notes = "退款")
    @PostMapping("/refund")
    public R<ChinaumsLogVO> refund(@RequestBody ChinaumsLogDTO chinaumsLog) {
        log.info("refund - 退款 chinaumsLog：{}",chinaumsLog);
        return R.success(baseService.refund(chinaumsLog));
    }

    /**
     * 退款状态查询
     * @param chinaumsLog ChinaumsLogDTO
     * @return R
     */
    @ApiOperation(value = "退款状态查询", notes = "退款状态查询")
    @PostMapping("/queryRefund")
    public R<ChinaumsLogVO> queryRefund(@RequestBody ChinaumsLogDTO chinaumsLog) {
        log.info("queryRefund - 退款状态查询 chinaumsLog：{}", chinaumsLog);
        try {
            return R.success(baseService.queryRefund(chinaumsLog));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 支付回调
     * @param orderNumber 单号
     * @param orderType 类型
     * @return String
     */
    @PostMapping("/receiveNotify")
    public String receiveNotify(@RequestParam("orderNumber") String orderNumber,@RequestParam("orderType") TransactionOrderOrderTypeEnum orderType){
        ContextUtil.setProjectId(projectId);
        log.info("receiveNotify - 支付回调 orderNumber：{},orderType：{},",orderNumber,orderType);
        ChinaumsLogDTO chinaumsLog = new ChinaumsLogDTO();
        chinaumsLog.setOrderNum(orderNumber);
        chinaumsLog.setOrderType(orderType);
        baseService.queryPayStatus(chinaumsLog);
        return "SUCCESS";
    }

    /**
     * 支付回调2
     * @param chinaumsLog chinaumsLog
     * @return String
     */
    @PostMapping("/receiveNotify2")
    public String receiveNotify(@RequestBody ChinaumsLogDTO chinaumsLog){
        ContextUtil.setProjectId(projectId);
        log.info("receiveNotify - 支付回调 chinaumsLog：{},",chinaumsLog);
        baseService.queryPayStatus(chinaumsLog);
        return "SUCCESS";
    }

}
